PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

The A Reference To Operator

To create a variable whose value is a reference instead of the value of the object specified by a reference, use the A Reference To operator. Here's an example:

set myWindow to a reference to window "ASP Control Panel Report" ¬
    of application "Apple System Profiler"

The value of the variable myWindow is the reference

window "ASP Control Panel Report" of application "Apple System Profiler"

After you create a variable whose value is a reference, you can use it in a script anywhere a reference is required. When AppleScript executes the statement containing the variable, it replaces the variable with the reference. For example, when AppleScript executes the statement

tell myWindow
    get name --result: "ASP Control Panel Report"
end tell

it replaces the variable myWindow with the reference window "ASP Control Panel Report" of application "Apple System Profiler".

SYNTAX
[a] ( ref [to] | reference to ) reference

where reference is a reference to an object.

EXAMPLES

As indicated in the syntax description, there are many ways to shorten expressions containing A Reference To. For example, all of these expressions are equivalent:

set myWindow to a reference to the window "Report" ¬
    of the application "Apple System Profiler"

set myWindow to reference to window "Report" ¬
    of application "Apple System Profiler"

set myWindow to a ref window "Report" ¬
    of application "Apple System Profiler"

set myWindow to ref window "Report" ¬
    of application "Apple System Profiler"

By using the abbreviation app, you can even fit the statement on one line:

set myWindow to ref window "Report" of app "Apple System Profiler"

As always, it's up to you to decide how conversational to make your script statements.

NOTES

You can use the A Reference To operator to avoid large copy operations. For example, suppose that you want to transfer a large image from one application to another application or script. If your script calls on the first application to create a copy of the image, then passes the copy on to the second application or script, the copy may require a large block of memory. Instead, your script can ask for a reference to the image, pass the reference, and let the application transfer the data directly.

You can also use the A Reference To Operator to access items in a large list efficiently. For example, the following script required 26 seconds to access all the items in a list of 4,000 integers (the time may vary based on the computer used and the version of AppleScript):

-- bigList is a list of 4,000 integers
set numItems to 4000
set t to (time of (current date))
repeat with n from 1 to numItems
    item n of bigList
end repeat
set total to (time of (current date)) - t
total --result: 26 seconds (depends on computer and AppleScript version)

The following script performs the same 4,000 access operations in approximately one second, using the A Reference To operator:

-- bigList is a list of 4,000 integers
set bigListRef to a reference to bigList
set numItems to 4000
set t to (time of (current date))
repeat with n from 1 to numItems
    item n of bigListRef
end repeat
set total to (time of (current date)) - t
total --result: 1 second (time may vary)

After you create a reference with the A Reference To operator, you can use the Contents property to get the value of the object that it refers to. The Contents property is the value of the object specified by a reference. For example, the result of getting the Contents property in the following script is the window reference itself.

set myWindow to a reference to window ¬
    "ASP Control Panel Report" of application "Apple System Profiler"
contents of myWindow
--result:
window "ASP Control Panel Report" of application "Apple System Profiler"

The following example gets a reference to a window's name:

set myWindow to a reference to name of window ¬
    "ASP Control Panel Report" of application "Apple System Profiler"
--result: name of window "ASP Control Panel Report"
--          of application "Apple System Profiler"

Getting the Contents property of the name reference returns a name string:

contents of myWindow --result: "ASP Control Panel Report"

For more information on the Contents property, see Reference.


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)